<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
	$data = array();

	if (Auth::check()) {
		$data = Auth::user();
	}
	return View::make('user', array('data'=>$data));
});

/**
 * use the Facebook SDK to redirect the user to a Facebook Auth page 
 * where the user can grant permissions for Facebook to pass in the email address and other data back to the application
 */
Route::get('login/fb', function() {
	$facebook = new Facebook(Config::get('facebook'));
	$params = array(
		'redirect_uri' => url('/login/fb/callback'),
		'scope' => 'email',
	);
	// redirect to external URL with no validation
	return Redirect::away($facebook->getLoginUrl($params));
});

Route::get('login/fb/callback', function() {
	$code = Input::get('code');
	if (strlen($code) == 0) 
		return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');

	$facebook = new Facebook(Config::get('facebook'));
	$uid = $facebook->getUser();

	if ($uid == 0) 
		return Redirect::to('/')->with('message', 'There was an error');

	$me = $facebook->api('/me');

	$profile = Profile::whereUid($uid)->first();
    if (empty($profile)) {

        $user = new User;
        $user->name = $me['last_name'] . ' ' . $me['middle_name'] . ' ' . $me['first_name'];
        $user->email = $me['email'];
        $user->photo = 'https://graph.facebook.com/' . $uid . '/picture?type=large';
        $user->save();

        $profile = new Profile();
        $profile->uid = $uid;
        $profile->username = $me['name'];
        $profile = $user->profiles()->save($profile);
    }

    $profile->access_token = $facebook->getAccessToken();
    $profile->save();

    $user = $profile->user;
    
    // Login an existing user facebook into your application
    Auth::login($user);

    return Redirect::to('/')->with('message', 'Logged in with Facebook');
});

Route::get('logout', function() {
	Auth::logout();
	return Redirect::to('/');
});



/**
 * Self-destructing, Encrypted Message App
 */
Route::get('/message', ['as' => 'messages.create', 'uses' => 'MessagesController@create']);
Route::post('/message', ['as' => 'messages.store', 'uses' => 'MessagesController@store']);
//Route::get('/{url}/{key}', ['as' => 'messages.show', 'uses' => 'MessagesController@show']);
Route::get('/message/{key}/{value}', ['as' => 'messages.show', 'uses' => 'MessagesController@show']);



/* Model Bindings */
/* 
Laravel has a nice little feature for binding models to the route parameters.
Route::get('post/{post}',function(Post $post) {
    echo $post->title;
});
Now when we will visit this route with a Post id as a parameter, A Post model corresponding to this id will be automatically injected to the route handler closure / controller action.
*/
Route::model('post', 'Post');
Route::model('comment', 'Comment');

/* User routes */
Route::get('/post/{post}/show', 	['as' => 'post.show', 	'uses' => 'PostController@showPost']);
Route::post('/post/{post}/comment', ['as' => 'comment.new', 'uses' => 'CommentController@newComment']);

/* Admin routes */
Route::group(['prefix' => 'admin', 'before' => 'auth'], function () {
	Route::get('dash-board', function () {
        $layout = View::make('master');
   	    $layout->title = 'DashBoard';
        $layout->main = View::make('dash')->with('content', 'Hi admin, Welcome to Dashboard!');
        return $layout;
  	});
     
	Route::get('/post/list', 			['as' => 'post.list', 	'uses' => 'PostController@listPost']);
    Route::get('/post/new', 			['as' => 'post.new', 	'uses' => 'PostController@newPost']);
    Route::get('/post/{post}/edit', 	['as' => 'post.edit', 	'uses' => 'PostController@editPost']);
    Route::get('/post/{post}/delete', 	['as' => 'post.delete', 'uses' => 'PostController@deletePost']);
    Route::post('/post/save', 			['as' => 'post.save', 	'uses' => 'PostController@savePost']);
    Route::post('/post/{post}/update', 	['as' => 'post.update', 'uses' => 'PostController@updatePost']);
      
    Route::get('/comment/list', 				['as' => 'comment.list', 	'uses' => 'CommentController@listComment']);
    Route::get('/comment/{comment}/show', 		['as' => 'comment.show', 	'uses' => 'CommentController@showComment']);
    Route::get('/comment/{comment}/delete', 	['as' => 'comment.delete', 	'uses' => 'CommentController@deleteComment']);
    Route::post('/comment/{comment}/update', 	['as' => 'comment.update', 	'uses' => 'CommentController@updateComment']);

 });

/* Match all Action for BlogController */
//Route::controller('/', 'BlogController');

/* View Composer */
// Whenever a view is rendered for which a callback is registered, 
// Laravel will execute the callback before rendering the view.
View::composer('sidebar', function ($view) {
    $view->recentPosts = Post::orderBy('id', 'desc')->take(5)->get();
});


// Route::get('register', array(
// 	'uses' => 'RegisterController@index',
// 	'as' => 'register.index'
// ));
// Route::post('register', array(
// 	'uses' => 'RegisterController@store',
// 	'as' => 'register.store'
// ));
// Route::resource('nerds', 'NerdController');





